#pragma once
#include <iostream>
using namespace std;
#include <queue>
#include <vector>
#include <functional>
namespace bit
{
	template<class T>
	struct less
	{
		int operator()(T& p,T& q)
		{
			return p > q;
		}
	};
	template<class T,class Container = vector<T>,class Com = bit::less<T>>
	class priority_queue
	{
	public:
		template<class Input_iterator>
		priority_queue()
			:_c()
		{}

		priority_queue(Input_iterator first, Input_iterator last)
			:_c(first, last)
		{
			int size = _c.size();
			for (int i = (size - 1 - 1) / 2; i >= 0; i--)
			{
				adjustdown(_c, i,size);
			}
		}

		bool empty()const
		{
			return _c.size() == 0;
		}
		size_t size()const
		{
			return _c.size();
		}
		const T& top()const
		{
			return _c.front();
		}
		void pop()
		{
			std::swap(_c[0], _c[_c.size() - 1]);
			adjustdown(_c, 0, _c.size() - 1);
			_c.pop_back();
		}
		void push(const T& val)
		{
			_c.push_back(val);
			adjustup(_c, _c.size() - 1);
		}
		void adjustup(Container& c, int child)
		{
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (_com(c[child], c[parent]))
					std::swap(c[parent], c[child]);
				else
					break;
				child = parent;
				parent = (child - 1) / 2;
			}
		}
		void adjustdown(Container& c,int parent,int size)
		{
			int child = 2 * parent + 1;
			while (child<size)
			{
				if (child + 1 < size && _com(c[child + 1],c[child]))
				{
					child++;
				}
				if (_com(c[child], c[parent]))
				{
					std::swap(c[parent], c[child]);
				}
				else
				{
					break;
				}
				parent = child;
				child = 2 * parent + 1;
			}
		}
	private:
		Container _c;
		Com _com;
	};
}
